package com.my.trace;

import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.android.gms.maps.SupportMapFragment;
import com.my.chart.ChartView;

public class MainActivity extends ActionBarActivity {
    final String TAG = "MainActivity";

    TextView tv_Speed;
    TextView tv_Lat;
    TextView tv_Lon;
    TextView tv_Alt;

    //ArrayList<TrackPoint> trackLog;
    TrackPoints trackPoints;

    Tracker tracker;
    ChartView chartView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
        //
        tv_Speed = (TextView) findViewById(R.id.textView_Speed);
        //tv_Lat = (TextView)findViewById(R.id.textView_Lat);
        //tv_Lon = (TextView)findViewById(R.id.textView_Lon);
        tv_Alt = (TextView)findViewById(R.id.textView_Alt);
        //
        tracker = new Tracker(this);
        tracker.setGoogleMap(((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap());
        tracker.addUpdateListener(new Tracker.LocationUpdateListener(){
            @Override
            public void updateLocation(Location location) {
                updateWithNewLocation(location);
            }
        });
        //
        //chartView = (ChartView) findViewById(R.id.chartView);
        //chartView.setCharRender(ChartView.ChartType.CHART_LINE);
        //trackLog = new ArrayList<TrackPoint>();
        //Log.d(TAG, "empty?" +trackLog.isEmpty());

        trackPoints = new TrackPoints();

        //chartView = new ChartView(this, null);
        chartView = (ChartView)findViewById(R.id.chartView);
 //       chartView.setTrackPoints(trackPoints);
 //       chartView.setLineChart(Color.YELLOW);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }
    int nVal = 0;
    //
    private void updateWithNewLocation(Location location) {
        //TrackPoint trackPoint = new TrackPoint(location);
        //trackPoint.altitude = (nVal++)*10;
        //trackLog.add(trackPoint);
        trackPoints.addPoint(location);

        Log.d(TAG, "updateWithNewLocation " + trackPoints.getSize());

        tv_Speed.setText("Speed(KPH):\n\r" + (int)(trackPoints.getVal(TrackPoints.VAL_SPEED, trackPoints.getSize())*3.6)); //in KPH
        //tv_Lat.setText("Latitude:\n\r" + trackPoints.getVal(TrackPoints.VAL_LATITUDE, trackPoints.getSize()));
        //tv_Lon.setText("Longitude:\n\r" + trackPoints.getVal(TrackPoints.VAL_LONGITUDE, trackPoints.getSize()));
        tv_Alt.setText("Altitude(M):\n\r" + trackPoints.getVal(TrackPoints.VAL_ALTITUDE, trackPoints.getSize()));

        chartView.update(trackPoints);
        //chartView.draw(0, trackLog.size());
        //chartView.invalidate();

    }
}
